<?php
// event/Form/Event.php

class event_Form_Event extends Zend_Form
{

    protected $_noDecorator = array(
        'ViewHelper', 
        'Errors'
    );

    public function cluBuild($params)
    {
    	$this->setMethod('POST');
    	if ($params['action'] === 'new')
		{
            $this->setAttrib('id', 'new_event');
//            $e = $params['view']->hvnUrl(array('module' => 'event', 'action'=>'new', 'view' => $this->view));
//        	$this->setAction($e);
		}
		else
    	{
            $this->setAttrib('id', 'edit_event');
//            $e = $params['view']->hvnUrl(array('module' => 'event', 'action'=>'edit', 'view' => $this->view));
//        	$this->setAction($e);
		}	
		$this->setAction($params['action_url']);
		
    	$filterTrim = new Zend_Filter_StringTrim();
    	$filterHtmlBody = new Clu_Filter_HtmlBody();
		$this->addElementPrefixPath('Clu_Filter', 'Clu/Filter/', 'filter');
		
		//add title
        $title = new Zend_Form_Element_Text('title', 
                         array('disableLoadDefaultDecorators' => true,
//                             'required'   => true,             
                               'class' => 'inputText'));
        
        $title->addDecorators($this->_noDecorator);
        $this->addElement($title);
        
        //add keywords
        $keywords = new Zend_Form_Element_Text('keywords', 
                         array('disableLoadDefaultDecorators' => true,
//                             'required'   => true,             
                               'class' => 'inputText'));
        $keywords->addDecorators($this->_noDecorator);
        $this->addElement($keywords);
        
        //add category
        $category = new Zend_Form_Element_MultiCheckbox('category_event',  
                         array('disableLoadDefaultDecorators' => true,
                         'required'   => true,  
                         ));
    	$mCategory = new category_Model_Category();
        $mCategory->setTables('event');
        foreach($mCategory->getRootType('Root_Event') as $e) {
            $category->addMultiOption($e['id'], p($e['name']));
        }             
        $category->addDecorators($this->_noDecorator);
        $this->addElement($category);
        
        if ($params['action'] === 'new')
		{
			if(strpos ($params['userPermissions'], 'set status event') === false)
			{
    			//add check captcha
                $elementCaptcha = 
                				   new Zend_Form_Element_Captcha('captcha_newevent',
                                   array('captcha' => 'Image','captchaOptions' =>
                                   array(
                                        'captcha' => 'Image',
                                        'wordLen' => 4,
                                        'timeout' => 300,
                                        'imgDir'  =>APP_PATH.'/public/captcha',
                                        'imgUrl' => '/public/captcha/',
                                        'width' => 175,
                                        'height' => 60,
                                        'font' => APP_PATH.'/public/captcha/font/tahoma.ttf',
                                        'DotNoiseLevel' => 10,  
                                        'LineNoiseLevel' => 1,
                                        'expiration' => 3,
                                        'gcFreq' => 1))
                );
                $elementCaptcha->setLabel(t(39).':')
                               ->setAttrib('id','captcha_neweventImage')
                           	   ->setRequired(true);
                $this->addElement($elementCaptcha);
			}
			    
            //add language
            $eventLanguage = new Zend_Form_Element_Select('eventLanguage',  
                             array('disableLoadDefaultDecorators' => true,
    //                                'required' => true
                             )); 
            $eventLanguage->addMultiOption('vi',t(40))
                           ->addMultiOption('en',t(41))
                           ->addMultiOption('cn',t(42))
                           ->addMultiOption('jp',t(43))
                           ->addDecorators($this->_noDecorator);
            $this->addElement($eventLanguage);
		}    
		
		//status
        //only the user with permission can set the status of the event.
        //else, it will be null and default values 'queued' will be inserted to DB
        if (strpos ($params['userPermissions'], 'set status event'))
        {
            $status = new Zend_Form_Element_Select('status',  
                             array('disableLoadDefaultDecorators' => true));
            $status->addMultiOption('featured',t(33))
            	   ->addMultiOption('published',t(34))
                   ->addMultiOption('queued',t(35))
                   ->addMultiOption('unpublished',t(36))
                   ->addMultiOption('deleted',t(37))
                   ->addDecorators($this->_noDecorator);
    		$status->setValue('published');
            $this->addElement($status);
        }     
        
        //add frequency
        $frequency = new Zend_Form_Element_Select('frequency',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
        $frequency->addMultiOption('once'   ,t(44))
        	 ->addMultiOption('daily'  ,t(45))
             ->addMultiOption('weekly' ,t(46))
             ->addMultiOption('monthly',t(47))
             ->addMultiOption('yearly' ,t(48))
             ->addDecorators($this->_noDecorator);
        $this->addElement($frequency);
        
        //add calendarType
        $calendarType = new Zend_Form_Element_Select('calendarType',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
        $calendarType->addMultiOption('solar',t(49))
                 ->addMultiOption('lunar'   ,t(50))
                 ->addDecorators($this->_noDecorator);
        $this->addElement($calendarType);
        
        //add start hour
        $startHour = new Zend_Form_Element_Select('startHour',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $startHour->addDecorators($this->_noDecorator)
					;
		for($i = 0; $i <=  23; $i++)
        	$startHour->addMultiOption($i,$i);
       	$startHour->setValue('8');
        $this->addElement($startHour);

        //add finish hour
        $finishHour = new Zend_Form_Element_Select('finishHour',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $finishHour->addDecorators($this->_noDecorator)
					;
		for($i = 0; $i <=  23; $i++)
        	$finishHour->addMultiOption($i,$i);
        $finishHour->setValue('17');
        $this->addElement($finishHour);
        
        //add start minute
        $startMinute = new Zend_Form_Element_Select('startMinute',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $startMinute->addDecorators($this->_noDecorator);
        for($i = 0; $i <=  55; $i+=5)
    		$startMinute->addMultiOption($i,$i);
        $this->addElement($startMinute);
        
        //add finish minute
        $finishMinute = new Zend_Form_Element_Select('finishMinute',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $finishMinute->addDecorators($this->_noDecorator);
        for($i = 0; $i <=  55; $i+=5)
    		$finishMinute->addMultiOption($i,$i);
        $this->addElement($finishMinute);
        
        //add start day
        $startDay = new Zend_Form_Element_Select('startDay',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $startDay->addMultiOption(2,t(51))
            	   ->addMultiOption(3,t(52))
                   ->addMultiOption(4,t(53))
                   ->addMultiOption(5,t(54))
                   ->addMultiOption(6,t(55))
                   ->addMultiOption(7,t(56))
                   ->addMultiOption(8,t(57))
        		   ->addDecorators($this->_noDecorator);
        $this->addElement($startDay);
        
        //add finish day
        $finishDay = new Zend_Form_Element_Select('finishDay',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $finishDay->addMultiOption(2,t(51))
            	   ->addMultiOption(3,t(52))
                   ->addMultiOption(4,t(53))
                   ->addMultiOption(5,t(54))
                   ->addMultiOption(6,t(55))
                   ->addMultiOption(7,t(56))
                   ->addMultiOption(8,t(57))
        		   ->addDecorators($this->_noDecorator);
        $this->addElement($finishDay);
        
        //add start date
        $startDate= new Zend_Form_Element_Text('startDate',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $startDate->addDecorators($this->_noDecorator);
        $startDate->size = 10;
        $this->addElement($startDate);
        
        //add finish date
        $finishDate = new Zend_Form_Element_Text('finishDate',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
        $finishDate->size = 10;
        $finishDate->addDecorators($this->_noDecorator);
        $this->addElement($finishDate);
        
        //add other frequency
        $fck = new Clu_Form_Element_FckEditor('other_frequency', array(
        		'fckOptions' => array (
        						'Height' => 150,
        						'Width'  => 500,
        						'ToolbarSet' => 'UserSignature'
        					)
       			 )
        );
        $fck->setLabel(t(429));
        $fck->addFilter($filterHtmlBody ); //TODO: if our people, maybe we don't need to filter
        $this->addElement($fck);
        
        //add description 
        $description = new Clu_Form_Element_FckEditor('description', array(
        		'fckOptions' => array (
        						'Height' => 600,
        						'Width'  => 620,
        						'ToolbarSet' => 'Full'
        					)
       			 )
        );
        $description->setLabel(t(206));
        
     	//if our people, maybe we don't need to filter
		if (strpos ($params['userPermissions'], 'set status event'))
		{
        	$description->addFilter($filterHtmlBody ); 
		}
		
        $this->addElement($description);
        
        // add the submit button
		if ($params['action'] === 'new')
    		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                           'label' => t(58) ));
      	else
      	$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                               'label' => t(59) ));
        
        $submit->addDecorators($this->_noDecorator);
        $this->addElement($submit);
        
        //if(!isset($params['place_id']))
        {
        	/*
	        //select place type
	         $selectPlaceType = new Zend_Form_Element_Select('select_place_type',  
	                         array('disableLoadDefaultDecorators' => true,
	                                'required' => true
	                         )); 
	         $selectPlaceType->addMultiOption('0','--'.t(716).'--')
	         				->addMultiOption('1', 'Address')
	         				->addMultiOption('2', 'Belong to Place')
	         				->addDecorators($this->_noDecorator);
	         $this->addElement($selectPlaceType);
	         */
	         //City
	        $city_id = new Zend_Form_Element_Select('city_id',array('disableLoadDefaultDecorators' => true,
	                                                             'required' => false));
	        $city_id->addMultiOption('0','--'.t(717).'--');
	        $table = new place_Model_City();
	        $results = $table->fetchAll();
//	        $rets = $results->toArray();
	        
	        $topCities = Zend_Registry::get('configuration')->topCities->toArray();
        	$city_id->addMultiOptions($topCities)
		   		    ->setAttrib('disable', array('1a'));
	
	        foreach ($results as $c) {
//	        	if (!array_key_exists($c['city_id'], $topCities)) //only add non-top cities
		            $city_id->addMultiOption($c['city_id'], $c['city_name']);
	        }
	    	$city_id->addDecorators($this->_noDecorator);
	        $this->addElement($city_id);
	        
	        //District
	        $district_id = new Zend_Form_Element_Select('district_id',array('disableLoadDefaultDecorators' => true,
	                                                                 'required' => false));
	        $district_id->addMultiOption('0','--'.t(718).'--')
	                    ->setRegisterInArrayValidator(false)	
	                    ->addDecorators($this->_noDecorator);
	 
	        $this->addElement($district_id);
	        
	        //Village
	        $village_id = new Zend_Form_Element_Select('village_id',array('disableLoadDefaultDecorators' => true,
	                                                                 'required' => false));
	        $village_id->addMultiOption('0','--'.t(720).'--')
	                   ->setRegisterInArrayValidator(false)	
	                   ->addDecorators($this->_noDecorator);
	 
	        $this->addElement($village_id);
	        
	        
	        // Street_Name
	        $str_name = new Zend_Form_Element_Text('address5', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $str_name->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim);
	        $this->addElement($str_name);
	        
	        // Street_Number
	        $str_number = new Zend_Form_Element_Text('address6', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $str_number->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim);
	        $this->addElement($str_number);
	        
	        // Thon_Khu Pho
	        $address7 = new Zend_Form_Element_Text('address7', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $address7->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim);
	        $this->addElement($address7);
	        
	        // Xom_Ap_Doi
	        $address8 = new Zend_Form_Element_Text('address8', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $address8->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim);
	        $this->addElement($address8);
	        
	        // The rest
	        $address9 = new Zend_Form_Element_Text('address9', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $address9->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim);
	        $this->addElement($address9);
        }	  
        
         // Category
 		$category = new Zend_Form_Element_Select('category',  
 											  array('disableLoadDefaultDecorators' => true,
									          ));
		$mCategory = new category_Model_Category();
		$mCategory->setTables('place');
        $category->addMultiOption('0','--'.t(113).'--');
        foreach ($mCategory->getRootType('Root_Place') as $c) {
        	if($c['id'] != 89)
        	{
	            $category->addMultiOption($c['id'], $c['name']);
	        	$category->addDecorators($this->_noDecorator);
        	}
        }
        $this->addElement($category);
        
        //rtype
        $rtype = new Zend_Form_Element_Select('rtype',  array('disableLoadDefaultDecorators' => true,
                                                                 'required' => false));
		$rtype->addMultiOption('0','--'.t(112).'--');                                                                 
		$rtype->setRegisterInArrayValidator(false)
			  ->addDecorators($this->_noDecorator);
        $this->addElement($rtype);
    }
    
	public function populateEventForm($data)
    {
    	$mDistrict = new place_Model_District();
		$mVillage = new place_Model_Village();
		$m = new event_Model_Service();
        //Populate district options
        if(isset($data['city_id']))
        { 
        	$cityId = $data['city_id'];
	    	if ($cityId != 0)
		    {
			    $districts = $mDistrict->getDistricts($cityId);
				foreach ($districts as $d) {
	          	    $this->district_id->addMultiOption($d['district_id'], $d['district_name']);
	     		}
				
	        }
        }
        //populate village options
        if(isset($data['district_id']))
        {
        	$districtId = $data['district_id'];
	    	if ($districtId != 0)
		    {
			    $villages = $mVillage->getVillages($districtId);
				foreach ($villages as $v) {
	          	    $this->village_id->addMultiOption($v['village_id'], $v['village_name']);
	     		}
	        }
        }
        
        if(!empty($data['id']))
        	$this->populate($m->transformToFormArray(true, $data));
        
    }
}
?>
